package com.chinasoft.mall.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.widget.Toast;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.adapter.StaffGoodsListAdapter;
import com.chinasoft.mall.framework.BeanCache;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.framework.ControllerCenter;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.EmployeeGoodsByClass;
import com.chinasoft.mall.model.response.GoodsByClassBean;
import com.chinasoft.mall.widget.LoadListView;
import com.chinasoft.mall.widget.LoadListView.OnRefreshListener;
/**
 * 员工商城分类列表
 * @author Administrator
 *
 */
public class StaffGoodsListActivity extends BaseActivity implements OnRefreshListener{
	private LoadListView modellist;
	private BaseBean goodsbyclass_bb;
	private StaffGoodsListAdapter mlAdapter;
	private List<EmployeeGoodsByClass> gbcb=new ArrayList<EmployeeGoodsByClass>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.staff_goods_list);
		initView();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		initRequest();
	}

	private void initRequest() {
		goodsbyclass_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_GOODSBYCLASSID);
		if (goodsbyclass_bb != null) {
			success(goodsbyclass_bb);
		} else {
			Toast.makeText(this, "数据获取失败", Toast.LENGTH_LONG).show();
			((MainActivity) this.getParent()).backCurrentActivity(false);
		}
	}

	private void initView() {
		gbcb.clear();
		modellist = (LoadListView) findViewById(R.id.modellist);
		mlAdapter = new StaffGoodsListAdapter((MainActivity) this.getParent(),
				null, this.getClass());
		modellist.setAdapter(mlAdapter);
		modellist.setonRefreshListener(this);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if(obj.equals(goodsbyclass_bb)){
			if(obj.getResData()!=null&&obj.getResData().getData()!=null){
				gbcb.addAll((List<EmployeeGoodsByClass>)obj.getResData().getData());
				mlAdapter.setData(gbcb);
				modellist.onRefreshComplete();
			}
		}
	}

	@Override
	public void onRefresh() {
		goodsbyclass_bb.reqData.put("class_id", (Integer.parseInt(goodsbyclass_bb.reqData.get("pageNo"))+1)+"");
		control.postMessage(goodsbyclass_bb);
	}
}
